package c.a.a;

import android.os.Handler;
import android.os.Looper;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/* compiled from: LottieTask */
public class t<T> {
    public static Executor e = Executors.newCachedThreadPool();
    public final Set<n<T>> a = new LinkedHashSet(1);
    public final Set<n<Throwable>> b = new LinkedHashSet(1);
    public final Handler c = new Handler(Looper.getMainLooper());
    public volatile r<T> d = null;

    /* compiled from: LottieTask */
    public class a extends FutureTask<r<T>> {
        public a(Callable<r<T>> callable) {
            super(callable);
        }

        public void done() {
            if (!isCancelled()) {
                try {
                    t.this.c((r) get());
                } catch (InterruptedException | ExecutionException e) {
                    t.this.c(new r(e));
                }
            }
        }
    }

    public t(Callable<r<T>> callable, boolean z) {
        if (z) {
            try {
                c((r) callable.call());
                return;
            } catch (Throwable th) {
                c(new r(th));
                return;
            }
        }
        e.execute(new a(callable));
    }

    public synchronized t<T> a(n<Throwable> nVar) {
        if (!(this.d == null || this.d.b == null)) {
            nVar.onResult(this.d.b);
        }
        this.b.add(nVar);
        return this;
    }

    public synchronized t<T> b(n<T> nVar) {
        if (!(this.d == null || this.d.a == null)) {
            nVar.onResult(this.d.a);
        }
        this.a.add(nVar);
        return this;
    }

    public final void c(r<T> rVar) {
        if (this.d == null) {
            this.d = rVar;
            this.c.post(new s(this));
            return;
        }
        throw new IllegalStateException("A task may only be set once.");
    }
}
